/*
 * Author Peter Mosher
 */
package model;

import java.io.Serializable;
import java.util.ArrayList;

/**
 * The super class data structure Users that contains all
 * the users.
 * @author Peter Mosher
 *
 */
public class Users implements Serializable{
	/**
	 * The Constant serialVersionUID.
	 */
	private static final long serialVersionUID = 
		4245257201971171916L;
	/**
	 * The volunteers and group leaders are contained here.
	 */
	private ArrayList <Volunteer> myVolunteers;
	/**
	 * The Schedulers are contained here.
	 */
	private ArrayList <Scheduler> mySchedulers;
	/**
	 * Land Management Agency users are contained here.
	 */
	private ArrayList <LMA> myLMAs;

	/**
	 * Creates an empty Users object.
	 */
	public Users()
	{
		myVolunteers = new ArrayList<Volunteer>();
		mySchedulers = new ArrayList<Scheduler>();
		myLMAs = new ArrayList<LMA>();
	}
	
	/**
	 * Gives the Scheduler object from this class to 
	 * another class.
	 * @return Scheduler object
	 */
	public Scheduler getScheduler()
	{
		return mySchedulers.get(0);
	}
	
	/**
	 * Finds out where the User is.
	 * Deletes this user.
	 * @param theUser to be deleted.
	 * @return true if the operation was successful.
	 */
	public boolean removeUser(final User theUser)
	{
		if(theUser instanceof Volunteer)
		{
			if(myVolunteers.isEmpty())
			{
				return false;
			}
			else
			{
				myVolunteers.remove(theUser);
				return true;
			}
		}
		else if(theUser instanceof Scheduler)
		{
			if(mySchedulers.isEmpty())
			{
				return false;
			}
			else
			{
				mySchedulers.remove(theUser);
				return true;
			}
		}
		else if(theUser instanceof LMA)
		{
			if(myLMAs.isEmpty())
			{
				return false;
			}
			else
			{
				myLMAs.remove(theUser);
				return true;
			}
		}
		return false;
	}
	
	/**
	 * Determines if users exist or not.
	 * @return returns true if atleast one user exists.
	 */
	public boolean isEmpty()
	{
		if(
				myVolunteers.isEmpty() && 
				mySchedulers.isEmpty() && 
				myLMAs.isEmpty())
		{
			return true;
		}
			return false;
	}
	
	/**
	 * Finds a user by a specific name.
	 * If a user exists by that name they are returned.
	 * Otherwise this method returns null.
	 * @param theName a name of a user who may exist.
	 * @return a user by this name if it exists.
	 */
	public User findUserByName(final String theName)
	{
		if(theName == null)
		{
			return null;
		}	
		int i = 0;
		while(i < mySchedulers.size()) 
		{
			
			if(mySchedulers.get(i).getUserName().
					equals(theName))
			{ 
				//get clones the String already
				//need to clone the User!
				return mySchedulers.get(i);
			}
			i++;
		}
		i = 0;
		while(i < myVolunteers.size()) 
		{
			if(myVolunteers.get(i).getFirstName().
					equals(theName))
			{ 
				//get clones the String already
				//need to clone the User!
				return myVolunteers.get(i);
			}
			i++;
		}
		i=0;
		while(i < myLMAs.size()) 
		{
			if(myLMAs.get(i).getFirstName().equals(theName))
			{ 
				//get clones the String already
				//need to clone the User!
				return myLMAs.get(i);
			}
			i++;
		}
		return null;
	}
	
	/**
	 * Finds a user by a specific name.
	 * If a user exists by that name they are returned.
	 * Otherwise this method returns null.
	 * @param theName a name of a user who may exist.
	 * @return a user by this name if it exists.
	 */
	public User findUser(final String theName)
	{
		if(theName == null)
		{
			return null;
		}
		
		for(Scheduler s : mySchedulers)
		{
			if(s.getUserName().equals(theName))
			{
				return s;
			}
		}
		
		for(LMA l : myLMAs)
		{
			if(l.getUserName().equals(theName))
			{
				return l;
			}
		}
		
		for(Volunteer v : myVolunteers)
		{
			if(v.getUserName().equals(theName))
			{
				return v;
			}
		}
		
		
		return null;
	}
	
	/**
	 * finds a volunteer by name.
	 * @param theName a name of a user who may exist.
	 * @return a user by this name if it exists.
	 */
	public Volunteer findVolByName(final String theName)
	{
		//all volunteers, check
		int i = 0;
		while(i < myVolunteers.size()) 
		{
			if(myVolunteers.get(i).getFirstName().
					equals(theName))
			{ 
				//get clones the String already
				//need to clone the User!
				return myVolunteers.get(i);
			}
			i++;
		}
		return null;
	}
	
	/**
	 * Finds a Scheduler by name.
	 * @param theName a name of a user who may exist.
	 * @return a user by this name if it exists.
	 */
	public Scheduler findSchByName(final String theName)
	{
		//all schedulers, check
		int i = 0;
		while(i < mySchedulers.size()) 
		{
			
			if(mySchedulers.get(i).getFirstName().
					equals(theName))
			{ 
				//get clones the String already
				//need to clone the User!
				return mySchedulers.get(i);
			}
			i++;
		}
		return null;
	}
	
	/**
	 * Sends the group of Land Management Agency users,
	 * to another class.
	 * @return all land management agency users.
	 */
	public ArrayList <LMA> getLMAs()
	{
		return myLMAs;
	}
	
	/**
	 * Sends the list of all Volunteers to another class.
	 * @return returns all volunteer users.
	 */
	public ArrayList <Volunteer> getVolunteers()
	{
		return myVolunteers;
	}
	
	/**
	 * Finds a LMA by a specific name if that user exists.
	 * @param theName the name in question.
	 * @return returns the user or null if not found.
	 */
	public LMA findLMAByName(final String theName)
	{
		int i=0;
		while(i < myLMAs.size()) 
		{
			if(myLMAs.get(i).getFirstName().equals(theName))
			{ 
				//get clones the String already
				//need to clone the User!
				return myLMAs.get(i);
			}
			i++;
		}
		return null;
	}
	
	/**
	 * Adds a user to the Lists.
	 * @param theUser the user to be added.
	 * @return returns true if theUser is actually a 
	 * valid user type.
	 */
	public boolean addUser(final User theUser)
	{
		if(theUser instanceof LMA)
		{
			addLMA((LMA)theUser);
			return true;
		}
		if(theUser instanceof Scheduler)
		{
			addScheduler((Scheduler)theUser);
			return true;
		}
		if(theUser instanceof Volunteer)
		{
			addVolunteer((Volunteer)theUser);
			return true;
		}
		return false;
		
	}
	
	/**
	 * Adds an LMA to the list of users.
	 * @param theLMA the LMA user to be added.
	 * @return returns true if this LMA is not an empty
	 * user.
	 */
	public boolean addLMA(final LMA theLMA)
	{
		if(myLMAs == null)
		{
			return false;
		}
		else
		{
			myLMAs.add(theLMA);
			return true;
		}
	}
	
	/**
	 * Adds a Scheduler user to the list of users.
	 * @param theScheduler the user to be added.
	 * @return returns true if this Scheduler is not an 
	 * empty user.
	 */
	public boolean addScheduler(
			final Scheduler theScheduler)
	{
		if(mySchedulers == null)
		{
			return false;
		}
		else
		{
			mySchedulers.add(theScheduler);
			return true;
		}
	}
	
	/**
	 * Adds a Volunteer user to the list of users.
	 * @param theVol the user object to be added.
	 * @return returns true if this Volunteer is not an 
	 * empty user.
	 */
	public boolean addVolunteer(final Volunteer theVol)
	{
		if(myVolunteers == null)
		{
			return false;
		}
		else
		{
			myVolunteers.add(theVol);
			return true;
		}
	}
}
